package com.byd.bcqw

import android.app.Activity
import android.app.ActivityOptions
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider

private const val TAG = "MainActivity"
private const val KEY_INDEX = "index"
private const val REQUEST_CODE_CHET = 0

class MainActivity : BaseActivity() {
    private lateinit var trueButton: Button
    private lateinit var falseButton: Button
    private lateinit var nextButton: Button
    private lateinit var prevButton: Button
    private lateinit var questionTextView: TextView
    private lateinit var cheatButton: Button

    /*private val questionBank = listOf(
        Question(R.string.question_africa, true),
        Question(R.string.question_americas, true),
        Question(R.string.question_asia, false),
        Question(R.string.question_australia, false),
        Question(R.string.question_mideast, true),
        Question(R.string.question_oceans, true)
    )

    private var currentIndex = 0*/

    private val quizViewModel:QuizViewModel by lazy {
        ViewModelProvider(this).get(QuizViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        /*val provider = ViewModelProvider(this)
        val quizViewModel = provider.get(QuizViewModel::class.java)
        Log.d(TAG,"got a QuizViewModel:$quizViewModel")*/

        val current = savedInstanceState?.getInt(KEY_INDEX, 0) ?: 0
        quizViewModel.currentIndex = current

        trueButton = getView(R.id.true_button)
        falseButton = getView(R.id.false_button)
        nextButton = getView(R.id.next_button)
        questionTextView = getView(R.id.question_text_view)
        prevButton = getView(R.id.prev_button)
        cheatButton = getView(R.id.cheat_button)
        trueButton.setOnClickListener {
            //Toast.makeText(this,"true",Toast.LENGTH_SHORT).show()
            /*val toast = Toast.makeText(this,"true",Toast.LENGTH_SHORT)
            toast.setGravity(Gravity.TOP,0,0)
            toast.show()*/

            /*Toast.makeText(applicationContext, "True", Toast.LENGTH_SHORT).let {
                it.setGravity(Gravity.TOP, 0, 0)
                it.show()
            }*/
            checkAnswer(true)
        }

        falseButton.setOnClickListener {
            //Toast.makeText(applicationContext, "false", Toast.LENGTH_SHORT).show()
            checkAnswer(false)
        }

        nextButton.setOnClickListener {
            //currentIndex = (currentIndex + 1) % questionBank.size
            quizViewModel.moveToNext()
            updateQuestion()
        }

        prevButton.setOnClickListener {
            //currentIndex = (currentIndex - 1) % questionBank.size
            quizViewModel.moveToPrev()
            updateQuestion()
        }
        cheatButton.setOnClickListener {
            //val intent = Intent(this,CheatActivity::class.java)
            val answerIsTrue = quizViewModel.currentQuestionAnswer
            //startActivity(CheatActivity.newIntent(this,answerIsTrue))
            //startActivityForResult(CheatActivity.newIntent(this,answerIsTrue), REQUEST_CODE_CHET)
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                val options = ActivityOptions.makeClipRevealAnimation(it,0,0,it.width,it.height)
                startActivityIfNeeded(CheatActivity.newIntent(this,answerIsTrue), REQUEST_CODE_CHET,options.toBundle())
            }else{
                startActivityIfNeeded(CheatActivity.newIntent(this,answerIsTrue), REQUEST_CODE_CHET)
            }
        }
        updateQuestion()
    }

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        Log.i(TAG,"onSaveInstanceState")
        outState.putInt(KEY_INDEX,quizViewModel.currentIndex)
    }

    private fun updateQuestion() {
        /*val questionTextResId = questionBank[currentIndex].textResId
        questionTextView.setText(questionTextResId)*/
        questionTextView.setText(quizViewModel.currentQuestionText)
    }

    private fun checkAnswer(userAnswer: Boolean) {
        //val correctAnswer = questionBank[currentIndex].answer
        val correctAnswer = quizViewModel.currentQuestionAnswer
        /*val messageResId = if (userAnswer == correctAnswer) {
            R.string.correct_toast
        } else {
            R.string.incorrect_toast
        }*/
        if (count >= 0){
            val messageResId = if (userAnswer == correctAnswer) {
                R.string.correct_toast
            } else {
                R.string.incorrect_toast
            }
            Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show()
        }else{
            val messageResId = when{
                quizViewModel.isCheater -> R.string.judgment_toast
                userAnswer == correctAnswer -> R.string.correct_toast
                else -> R.string.incorrect_toast
            }
            Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show()
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK){
            return
        }
        if (requestCode == REQUEST_CODE_CHET){
            quizViewModel.isCheater = data?.getBooleanExtra(EXTRA_ANSWER_SHOWN,false) ?: false
        }
    }

}